package Create_Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author Kevin
 * @date 2020/4/2 22:34
 */
public class useCallableAndFuture{

    public static void main(String[] args) {
        //使用Lambda表达式创建Callable对象
        //使用FutureTask类来包装Callable对象
        FutureTask<Integer> future = new FutureTask<Integer>(
                (Callable<Integer>)() -> {
                    return 5;
                }
        );
        new Thread(future, "有返回值的线程").start();
        try {
            System.out.println("子线程的返回值： " + future.get());//get()方法会阻塞，直到子线程执行结束才返回
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

}
